package cn.ilikes.tools.nosql.jpa.springs;

import javax.persistence.spi.PersistenceUnitInfo;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;

//ResourceLoaderAware
public class DefultJPASQLPersistenceUnitManager implements PersistenceUnitManager, InitializingBean {

	private SpringPersistenceUnitProcessor springPersistenceUnitProcessor = new SpringPersistenceUnitProcessor();

	public final static String DEFAULT_PERSISTENCE_XML_LOCATION = "META-INF/persistence.xml";

	private String[] persistenceXmlLocations = new String[] { DEFAULT_PERSISTENCE_XML_LOCATION };

	private final static String ORIGINAL_DEFAULT_PERSISTENCE_UNIT_NAME = "default";

	public void setPersistenceXmlLocations(String... persistenceXmlLocations) {
		this.persistenceXmlLocations = persistenceXmlLocations;
	}

	@Override
	public PersistenceUnitInfo obtainDefaultPersistenceUnitInfo() throws IllegalStateException {
		return springPersistenceUnitProcessor.selectPersistenceUnitInfo(ORIGINAL_DEFAULT_PERSISTENCE_UNIT_NAME);
	}

	@Override
	public PersistenceUnitInfo obtainPersistenceUnitInfo(String arg0) throws IllegalArgumentException, IllegalStateException {

		return springPersistenceUnitProcessor.selectPersistenceUnitInfo(arg0);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		springPersistenceUnitProcessor.processor(persistenceXmlLocations);

	}

}